package com.ahui.util;

import com.ahui.domain.BondAccount;
import com.ahui.domain.ResultDataFGKDZ;
import com.ahui.service.FileProcess;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.List;

@Slf4j
@Data
public class ExcelUtil {

    public static void generateExcel(String exportPath, List<ResultDataFGKDZ> resultList) {
        //生成excel
        InputStream is = ExcelUtil.class.getClassLoader().getResourceAsStream("兴全提取结果整理.xlsx");
        File excelFile = new File(exportPath);
        FileOutputStream os;
        try {
            if (!excelFile.exists()){
                excelFile.createNewFile();
            }
            os = new FileOutputStream(excelFile);
            ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(is).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(resultList,fillConfig,writeSheet);
            excelWriter.finish();
            os.flush();
            os.close();
        }catch (Exception e){
            log.info("导出excel异常" + e);
            throw new RuntimeException();
        }
    }

    public static void bondGenerateExcel(String exportPath, List<BondAccount> resultList) {
        //生成excel
        InputStream is = ExcelUtil.class.getClassLoader().getResourceAsStream("账户信息表.xlsx");
        File excelFile = new File(exportPath);
        FileOutputStream os;
        try {
            if (!excelFile.exists()){
                excelFile.createNewFile();
            }
            os = new FileOutputStream(excelFile);
            ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(is).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(resultList,fillConfig,writeSheet);
            excelWriter.finish();
            os.flush();
            os.close();
        }catch (Exception e){
            log.info("导出excel异常" + e);
            throw new RuntimeException();
        }
    }
}